package com.navi168.fwq21.util;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Tools {
	
	public String getnowtime(){
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
	     return df.format(new Date());// new Date()为获取当前系统时间
	}
	
	
	public static double getDegreeDouble(byte[] degree) {
		int num = 1;
		byte duB = degree[0];
		byte fenB = degree[1];
		byte[] miaoB = new byte[2];
		miaoB[0] = degree[2];
		miaoB[1] = degree[3];
		int du = byteToInt(duB);
		int fen = byteToInt(fenB);
		int miao = seqToInt(miaoB);
		if (fen >= 128) {
			fen = fen - 128;
			num = -1;
		}
		return (du + (fen * 10000d + miao) / 60d / 10000d) * num;
	}

	public static byte[] getDegreeBytes(double degree) {
		int num = 1;
		if (degree < 0) {
			num = -1;
			degree = degree * -1;
		}
		byte[] d = new byte[4];
		int du = (int) degree;
		int fen = (int) ((degree - du) * 60);
		int miao = (int) Math.round((((degree - du) * 60 - fen) * 10000));
		if (num == -1) {
			fen = fen + 128;
		}
		byte duB = intToByte(du);
		byte fenB = intToByte(fen);
		byte[] miaoB = intToSeq(miao);
		d[0] = duB;
		d[1] = fenB;
		d[2] = miaoB[0];
		d[3] = miaoB[1];
		return d;
	}

	public static byte[] intToSeq(int data) {
		data = data & 0xffff;
		byte[] temp = new byte[2];
		for (int i = 0; i < 2; i++) {
			temp[i] = intToByte(data >>> (8 - i * 8));
		}
		return temp;
	}

	public static int seqToInt(byte[] data) {
		return ((byteToInt(data[0])) << 8) + byteToInt(data[1]);
	}

	public static int byteToInt(byte data) {
		int a = (int) data;
		a = a & 0xff;
		return a;

	}

	public static byte intToByte(int data) {
		data = data & 0xff;
		int temp = data;
		if (temp > 127) {
			data = temp - 256;
		}
		byte b = (byte) data;
		return b;
	}


}
